import apiClient from './index.js';
import axios from 'axios';
import { setLocalStorageWithExpiry , getLocalStorageWithExpiry} from '../stor/index.js'
import {useTokenStore} from '../stor/token.js'


/**
 * 查询
 * @param {*} url 
 * @param {*} params 
 * @param {*} headcontenttype 
 * @returns 
 */
export const  getApi =   (url,params,headcontenttype) => {
    const response =  apiClient({
        url:url,
        method:'get',
        params:params,
        contenttype:headcontenttype
    });
    return response;
}
/**
 * post查询方法
 * @param {*} url 
 * @param {*} data 
 * @returns 
 */
export const postApi = (url ,data,headcontenttype)=>{
    return apiClient({
        url:url,
        method:'post',
        data:data,
        contenttype:headcontenttype
    })
}
/**
 * 新增
 * @param {*} url 
 * @param {*} data 
 * @returns 
 */
export const insertApi = (url , data,headcontenttype)=>{
    return apiClient({
        url:url,
        method:'post',
        data:data,
        contenttype:headcontenttype
    })
}
/**
 * 更新方法
 * @param {*} url 
 * @param {*} params 
 * @returns 
 */
export const updateApi = (url , params,headcontenttype)=>{
    return apiClient({
        url:url,
        method:'put',
        data:params,
        contenttype:headcontenttype
    })
}
/**
 * 上传头像
 * @param {*} url 
 * @param {*} params 
 * @param {*} headcontenttype 
 * @returns 
 */
export const patchApi = (url , params,headcontenttype)=>{
    return apiClient({
        url:url,
        method:'patch',
        data:params,
        contenttype:headcontenttype
    })
}
/**
 * 删除方法
 * @param {*} url 
 * @param {*} data 
 * @returns 
 */
export const deleteApi = (url , data,headcontenttype)=>{
    return apiClient({
        url:url,
        method:'delete',
        data:data,
        contenttype:headcontenttype
    })
}

//同步等待服务器响应结果 并返回，
export  function searchData (url,params,headstr){

    if(headstr===null || headstr === undefined){
        headstr = 'application/x-www-form-urlencoded;charset=utf-8';
    }
    let headers={
           'Content-Type': headstr,
        }
    //const token = getLocalStorageWithExpiry("token");
    //在pinia中定义的响应式数据都不要用.value来获取值
    const tokenStore = useTokenStore();
    const token = tokenStore.token;
    if (token !== null && token !== undefined && token.length > 0) {
        headers={
            'Content-Type': headstr,
            'Authorization':token
         }
     }

    return  axios.get('/api'+url,{
        params:params,
        headers:headers
    })
    .then(result=>{
        return result.data;
    }).catch(err=>{
        console.log(err);
    })
}